package com.example.xianji.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.adapter.DuiJie_Adapter;
import com.example.xianji.BGARefresh.BGARefreshLayout;
import com.example.xianji.BGARefresh.BGARefreshLayout.BGARefreshLayoutDelegate;
import com.example.xianji.bean.My_DuiJie;

import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main_My_DuiJieActivity extends Activity implements
        BGARefreshLayoutDelegate {
    /*********************** 我对接的需求 ********************/
    private RelativeLayout my_duijie_back;
    //    private SharedPreferences sp;
//    private Editor ed;
    private ArrayList<My_DuiJie> duijie_list = new ArrayList<My_DuiJie>();
    private DuiJie_Adapter mDuiJie_Adapter;
    private BGARefreshLayout duijie_RefreshLayout;
    private ListView duijie_MyListview;
    private int page = 1;
    private TextView yincang_liebiao;

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    duijie_MyListview.setAdapter(mDuiJie_Adapter);
                    duijie_RefreshLayout.endRefreshing();
                    break;
                case 2:
                    mDuiJie_Adapter.notifyDataSetChanged();
                    duijie_RefreshLayout.endLoadingMore();
                    break;
                case 0:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__my__dui_jie);
        // XJApplication.getInstance().addActivity(this);
        my_duijie_back = (RelativeLayout) findViewById(R.id.my_relative_duijie_back);
        duijie_MyListview = (ListView) findViewById(R.id.duijie_MyListview);

        yincang_liebiao = (TextView) findViewById(R.id.yincang_liebiao);
        if (getIntent().getIntExtra("duijie", 0) == 2) {
            yincang_liebiao.setVisibility(View.GONE);
        } else if (getIntent().getIntExtra("duijie", 0) == 1) {
            yincang_liebiao.setVisibility(View.VISIBLE);
        }
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        mDuiJie_Adapter = new DuiJie_Adapter(this, duijie_list, 1, 1);
        duijie_RefreshLayout = (BGARefreshLayout) findViewById(R.id.duijie_RefreshLayout);
        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                get_duijie();
            }
        });

        my_duijie_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_My_DuiJieActivity.this.finish();
            }
        });

        yincang_liebiao.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 跳转到我隐藏对接的需求列表
                startActivity(new Intent(Main_My_DuiJieActivity.this,
                        Main_XuQiu_InVisibilityActivity.class));
            }
        });
        duijie_RefreshLayout.setDelegate(this);
        processLogic(savedInstanceState);
    }

    protected void processLogic(Bundle savedInstanceState) {
        duijie_RefreshLayout
                .setRefreshViewHolder(new com.example.xianji.BGARefresh.BGANormalRefreshViewHolder(
                        this, true));
    }

    public void get_duijie() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        parms.addBodyParameter("page", 1 + "");

        httpUtils.send(HttpMethod.POST, Contant.my_duijie, parms,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        duijie_RefreshLayout.endRefreshing();
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        try {
                            JSONObject object = new JSONObject(arg0.result);
                            if (object.getInt("status") == 1) {
                                duijie_list.clear();
                                JSONArray array = object.getJSONArray("data");
                                if (array.length() == 0) {
                                    Toast.makeText(Main_My_DuiJieActivity.this,
                                            object.getString("msg"), Toast.LENGTH_SHORT).show();
                                } else {
                                    for (int i = 0; i < array.length(); i++) {
                                        JSONObject obj = array.getJSONObject(i);
                                        My_DuiJie mMy_DuiJie = new My_DuiJie();
                                        mMy_DuiJie.setDemand_id(obj
                                                .getString("demand_id"));
                                        mMy_DuiJie.setTitle(obj
                                                .getString("title"));
                                        mMy_DuiJie.setStatus(obj
                                                .getInt("status"));
                                        mMy_DuiJie.setCreated_at(obj
                                                .getString("created_at"));
                                        duijie_list.add(mMy_DuiJie);
                                    }
                                }
                            }
                            handler.sendEmptyMessage(1);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public void get_duijie_more() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        parms.addBodyParameter("page", page + "");
        httpUtils.send(HttpMethod.POST, Contant.my_duijie, parms,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        duijie_RefreshLayout.endLoadingMore();
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        try {
                            JSONObject object = new JSONObject(arg0.result);
                            JSONArray array = object.getJSONArray("data");
                            if (array.length() > 0) {
                                for (int i = 0; i < array.length(); i++) {
                                    JSONObject obj = array.getJSONObject(i);
                                    My_DuiJie mMy_DuiJie = new My_DuiJie();

                                    mMy_DuiJie.setDemand_id(obj
                                            .getString("demand_id"));
                                    mMy_DuiJie.setTitle(obj.getString("title"));
                                    mMy_DuiJie.setStatus(obj.getInt("status"));
                                    mMy_DuiJie.setCreated_at(obj
                                            .getString("created_at"));
                                    duijie_list.add(mMy_DuiJie);
                                }
                            } else {
                                Toast.makeText(Main_My_DuiJieActivity.this,
                                        "加载完毕没有更多数据", Toast.LENGTH_SHORT).show();
                            }
                            handler.sendEmptyMessage(2);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        get_duijie();
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        page = 1;
        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                get_duijie();
            }
        });
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(
            BGARefreshLayout refreshLayout) {
        page++;
        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                get_duijie_more();
            }
        });
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
